package fileProcessing;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import audit.AuditWriter;

import field_REGEX_Definitions.Other.URL_Source;
import fileProcessing.PreprocessingAnalysis.FileAnalysis;

public class FilePreProcessor {
	private Pattern excludeLine = Pattern.compile("([\\d\\w])", Pattern.CASE_INSENSITIVE);
	private String currUser = ProjectDirectories.windowsCurrentUser;
	private File inPII;
	private String output_FilePath = ProjectDirectories.preprocessingDir;
	private String eol = "\r\n";
	private int tempFileCount = 0;
	public FileAnalysis preProcessing;
	public static AuditWriter audit;
	
	public FilePreProcessor(File inFile) throws ParsingException{
		inPII = inFile;
		audit = AuditWriter.getAuditWriter();
	}
	
	/**
	 * Compartmentalizes original file into one subfile per source 
	 * @throws FileNotFoundException
	 * @throws ParsingException 
	 */
	public void processFile() throws FileNotFoundException, ParsingException{
		Scanner inFile = new Scanner(new FileReader(inPII));
		
		output_FilePath+="\\";
		try {
			partitionFile(inFile);
			try {
				preProcessing = new FileAnalysis(output_FilePath);
			} catch (preprocessorException e) {
				audit.writeToAudit("While parsing file received error: " + e.getMessage());
				//System.out.println("While parsing file received error: " + e.getMessage());
			}
		} catch (IOException e) {
			audit.writeToAudit("File not found.");
		}
	}
	
	/**
	 * Breaks single file with multiple sections into separate files for each section
	 * @param inFile - Original text file with multiple collection sections
	 * @throws IOException
	 */
	public void partitionFile(Scanner inFile) throws IOException{
		BufferedWriter outFile = null;
		Matcher xLine;
		String nextLine;
		URL_Source header = new URL_Source();
		while(inFile.hasNext()){
			nextLine = inFile.nextLine();
			xLine = excludeLine.matcher(nextLine);
			if(header.matches(nextLine)){
				outFile = generateOutputFile(outFile);
				outFile.write(nextLine+eol);
			} 
			else if (xLine.find()){
				String temp,redundencyCheck = xLine.group(1);
				temp = nextLine.replace(redundencyCheck, "");
				xLine = excludeLine.matcher(temp);
				if(xLine.find()){
					if(outFile == null){
						outFile = generateOutputFile(outFile);
						outFile.write(nextLine+eol);
					}
					else
						outFile.write(nextLine+eol);
				}
			}
		}
		outFile.flush();
		outFile.close();
	}
	
	/**
	 * Writes new file for subsection contents
	 * @param fileContents Contents of subsection
	 * @throws IOException
	 */
	public BufferedWriter generateOutputFile(BufferedWriter prevFile) throws IOException{
		String fileName;
		BufferedWriter newFile;
		if(prevFile != null){
			fileName = generateOutputFilename();
			newFile = new BufferedWriter(new FileWriter(fileName));
			prevFile.flush();
			prevFile.close();
			return newFile;
		}
		else{
			fileName = generateOutputFilename();
			newFile = new BufferedWriter(new FileWriter(fileName));
			return newFile;
		}
	}
	
	/**
	 * Creates a new filename for the output file
	 * @return New filename
	 */
	public String generateOutputFilename(){
		String inc, fileName = output_FilePath+"source", ext = ".txt";
		tempFileCount++;
		inc = Integer.toString(tempFileCount);
		fileName+= inc+ext;
		return fileName;
	}
}
